package cc.ls51.cms.controller;


import cc.ls51.cms.utils.RenderUtil;
import cc.ls51.common.PageUtils;
import cc.ls51.form.SysRoleListForm;
import cc.ls51.modules.SysRole.entity.SysRole;
import cc.ls51.modules.SysRole.service.ISysRoleService;
import cc.ls51.vo.ResultObject;
import cn.hutool.core.util.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author llf
 * @since 2019-09-02
 */
@Controller
@RequestMapping("/SysRole")
public class SysRoleController {

    @Autowired
    RenderUtil renderUtils;

    @Autowired
    ISysRoleService sysRoleService;

    @RequiresPermissions("/SysRole/roleList")
    @RequestMapping(value = "/roleList")
    public String roleList(SysRoleListForm form, Model model) {
        PageUtils<SysRole> page = sysRoleService.getPage(form);

        model.addAttribute("page",page);
        return "/SysRole/roleList";
    }

    @RequestMapping("/roleEdit")
    public String roleEdit(String v, SysRole role,String ids,Model model) {
        ResultObject ro = ResultObject.ok();
        boolean bResult = false;
        if("add".equals(v)){
            return "/SysRole/roleEdit";
        }else if("save".equals(v)){
            bResult = sysRoleService.saveOrUpdate(role);

            if(!bResult){
                ro = ResultObject.error("保存错误");
            }
            renderUtils.renderJson(ro);
        }else if("del".equals(v)){
            if(StrUtil.isNotEmpty(ids)){
                bResult = sysRoleService.removeByIds(Arrays.asList(StrUtil.split(ids, ",")));
                if(!bResult){
                    ro = ResultObject.error("删除错误");
                }
            }
            renderUtils.renderJson(ro);
        }
        return "/SysRole/roleEdit";
    }
}
